package cn.javass.dp.memento.example3;

import java.io.*;

/**
 * 负责保存模拟运行流畅A的对象的备忘录对象
 * 离线存储   需要序列化 FlowAMockMemento
 */
public class FlowAMementoFileCareTaker {

    private FlowAMockMemento memento = null;

    public void savemMemento(FlowAMockMemento memento) {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(
                    new BufferedOutputStream(
                            new FileOutputStream("FlowAMemento")));
            out.writeObject(memento);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        this.memento = memento;
    }

    public FlowAMockMemento retriveMemento() {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(
                    new BufferedInputStream(
                            new FileInputStream("FlowAMemento")));
            memento = (FlowAMockMemento)in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return memento;
    }
}
